//////////////////////////////////////////////////////////////////////////////////////////////////
//	File: Animation.cpp
//
//	Author: Justin Maza
//
//	Purpose: Handle the animations of all game objects. Uses an anchor point to center all
//			 the images within their frame.
//////////////////////////////////////////////////////////////////////////////////////////////////

#include "Animation.h"
#include <tchar.h>

Animation::Animation(void)
{
	nImageID = -1;
	nCurrent_Frame = 0;
	bIsLooping	= false;
	bIsOscillating = false;
	bIsFlipped	= false;
	bIsPlaying = true;

	pTM = CSGD_TextureManager::GetInstance();
}


Animation::~Animation(void)
{
	if(nImageID != -1)
	{
		pTM->UnloadTexture(nImageID);
		nImageID = -1;
	}
	
}

void Animation::AddFrame(const char* fileName, RECT rFrame, POINT Anchor, float fDuration, bool bOscillating, bool bLooping, DWORD dwColor)
{

	nImageID = pTM->LoadTexture((const WCHAR*)(*fileName), dwColor);
	tFrame tempFrame;
	tempFrame.rFrame = rFrame;
	tempFrame.ptAnchor = Anchor;
	tempFrame.fDuration = fDuration;

	v_Frames.push_back(tempFrame);
	bIsLooping = bLooping;
	bIsOscillating = bOscillating;
	
}


void Animation::SetFramesM(const char* fileName, RECT rFrames[], POINT Anchors[], int nArraySize, float fDurations[], bool bOscillating, bool bLooping,int trigTypes[], string Triggers[], DWORD dwColor)
{
	size_t num;
	TCHAR buffer[120] = {};
	string path = "resource\\";
	path += fileName;

	mbstowcs_s(&num, buffer,120,path.c_str(),120);
	nImageID = CSGD_TextureManager::GetInstance()->LoadTexture(buffer,dwColor);
	tFrame framer;
	
	for(int i=0; i<nArraySize; ++i) //uses array size and RECT array parameters to load in each frame rather than using an algorithm
	{
		framer.rFrame = rFrames[i];
		framer.fDuration = fDurations[i];
		framer.ptAnchor = Anchors[i];
		framer.Trigger = Triggers[i];
		framer.Triggertype = trigTypes[i];

		v_Frames.push_back(framer);
	}

	bIsOscillating = bOscillating;
	bIsLooping = bLooping;
}

void Animation::SetFramesA(const char* fileName, int nInitialX, int nInitialY, int nWidth, int nHeight, int numCols, int numRows, POINT ptAnchor, bool bOscillating, bool bLooping, DWORD dwColor)
{

	tFrame framer;
	RECT rMyFrame;
	nImageID = pTM->LoadTexture((const WCHAR*)(fileName),dwColor);  // Load in Image to pull our sprites off of
	for(int x = 0; x < numCols; x++)
		for(int y = 0; y < numRows; y++)
		{
			rMyFrame.left = nInitialX + (nWidth * x); 
			rMyFrame.top = nInitialY + (nHeight * y);
			rMyFrame.right = rMyFrame.left + nWidth;
			rMyFrame.bottom = rMyFrame.top + nHeight;

			framer.rFrame = rMyFrame;
			framer.ptAnchor = ptAnchor;
			framer.fDuration = 0.2f;
			v_Frames.push_back(framer);				// Add the frame to the vector

		}
	
	
	bIsLooping = bLooping;
	bIsOscillating = bOscillating;
}
